﻿var IsLoadValidationError;
function LoadValidationErrorCss()
{
    
    if(IsLoadValidationError != null && IsLoadValidationError == true) return IsLoadValidationError;
    //Checking if ValidatorValidate exists
    //alert('Loaded');
    IsLoadValidationError = true;
    if (typeof (ValidatorValidate) != "undefined") {
        
        //Copy ValidatorValidate to BaseValidatorValidate
        BaseValidatorValidate = ValidatorValidate;
        //alert('validationerror 1');
        //Used to remeber the current activated validationGroup
        _lastValidationGroup = null;

        //Replacing ValidatorValidate
        ValidatorValidate = function(val, validationGroup, event) {
        //Calling original ValidatorValidate function saved in BaseValidatorValidate
           // alert('validationerror 2');
           
            BaseValidatorValidate(val, validationGroup, event);
            
            //Remembering the current validationGroup
            if (validationGroup != null) _lastValidationGroup = validationGroup;
            
            //Seting valid all the validations not in the current validationGroup
            if ((typeof (val.validationGroup) == "undefined" && _lastValidationGroup != "") || (typeof (val.validationGroup) != "undefined" && val.validationGroup != _lastValidationGroup)) val.isvalid = true;
            
            //***** To View Error Message *****//
            //val.errormessage = val.id.toString() + " " + validationGroup;
            //***********************************
            //alert('validationerror 3');
            //If a validationErrorCssClass is defined ...
            if (val.className || val.className.indexOf("validationErrorControl") != -1) {
                //Checking if all validators on the same controltovalidate with the same validationGroup are valid
                var flag1 = val.isvalid;
                
                for (var num1 = 0; num1 < Page_Validators.length; num1++) {
                    var val2 = Page_Validators[num1];
                    if (val2.controltovalidate == val.controltovalidate && (validationGroup == null || typeof (val2.validationGroup) == "undefined" || val2.validationGroup == validationGroup)) flag1 &= val2.isvalid;
                }

                //Adding or removing validationErrorCssClass from controltovalidate
                if (typeof ($get) != "undefined") {
                    //Do it with ASP.NET ajax functions
                    Sys.UI.DomElement[flag1 ? 'removeCssClass' : 'addCssClass']($get(val.controltovalidate), "validationError");
                }
                else {
                    //Do it with standard javascript without ajax functions
                    var el = document.getElementById(val.controltovalidate);
                    
                    if (el != null) {
                        var flag2 = el.className.indexOf("validationError") != -1;
                        if (flag1 && flag2) {
                            el.className = el.className.replace(new RegExp("\\b" + "validationError" + "\\b", "i"), "").replace(/^\s+|\s(?=\s+)|\s+$/m, "");
                        }
                        else if (!flag1 && !flag2) {
                            el.className += (el.className.length != 0 ? " " : "") + "validationError";
                        }
                    }
                }
            }

            //Refreshing ValidationSummary
            /*if (typeof (Page_ValidationSummaries) != "undefined" && _lastValidationGroup != null) {
                for (var num2 = 0; num2 < Page_ValidationSummaries.length; num2++) {
                    var pageVal = Page_ValidationSummaries[num2];
                    //If the ValidationSummary validationGroup is equal the last validationGroup updating it.
                    if ((typeof (pageVal.validationGroup) == "undefined" && _lastValidationGroup == "") || (typeof (pageVal.validationGroup) != "undefined" && pageVal.validationGroup == _lastValidationGroup)) {
                        setTimeout(function() { ValidationSummaryOnSubmit(_lastValidationGroup); }, 0);
                        break;
                    }
                }
            }*/
        }
        return IsLoadValidationError;
    }
}